extends Control

@onready var audio_player: AudioStreamPlayer = $AudioStreamPlayer

func _ready():
    setup_ui()

func setup_ui():
    # 创建控制界面
    var vbox = VBoxContainer.new()
    vbox.anchor_left = 0.5
    vbox.anchor_top = 0.5
    vbox.anchor_right = 0.5
    vbox.anchor_bottom = 0.5
    vbox.offset_left = -100
    vbox.offset_top = -50
    add_child(vbox)
    
    # 播放速度滑块
    var speed_label = Label.new()
    speed_label.text = "播放速度: 1.0x"
    vbox.add_child(speed_label)
    
    var speed_slider = HSlider.new()
    speed_slider.min_value = 0.1
    speed_slider.max_value = 3.0
    speed_slider.step = 0.1
    speed_slider.value = 1.0
    speed_slider.size_flags_horizontal = Control.SIZE_EXPAND_FILL
    vbox.add_child(speed_slider)
    
    # 播放按钮
    var play_button = Button.new()
    play_button.text = "播放音频"
    vbox.add_child(play_button)
    
    # 连接信号
    speed_slider.value_changed.connect(func(value):
        speed_label.text = "播放速度: " + str(value) + "x"
    )
    
    play_button.pressed.connect(func():
        play_with_variable_speed(speed_slider.value)
    )

func play_with_variable_speed(speed: float):
    var sound: AudioStream = preload("res://audio/rotating.ogg")
    
    # 重置播放位置到开头
    audio_player.stop()
    audio_player.stream = sound
    audio_player.pitch_scale = speed
    
    # 播放
    audio_player.play()
    
    print("以 %.1fx 速度播放音频" % speed)